package jp.co.cos.ittrick.channel.controller.admin.article;

import java.util.ArrayList;
import java.util.List;

import jp.co.cos.ittrick.channel.Constants;
import jp.co.cos.ittrick.channel.model.ArticleModel;
import jp.co.cos.ittrick.channel.service.ArticleService;

import org.apache.commons.lang.StringEscapeUtils;
import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;

import com.google.appengine.api.datastore.Text;

public class ArticleUpdateController extends Controller {

    private ArticleService articleService;

    public ArticleUpdateController() {
        articleService = new ArticleService();
    }

    @Override
    public Navigation run() throws Exception {

        // リクエストパラメータ
        String article = asString("article");
        String name = asString("articleName");
        String content = asString("content");
        String keyWords = asString("keyWord");
        String description = asString("description");

        List<String> keyWordList = getStringList(keyWords);

        // Modelの設定
        ArticleModel model = articleService.getArticleByKey(Integer.valueOf(article));
        if(model != null) {
            model.setName(name);
            model.setContent(new Text(content));
            model.setKeyWordList(keyWordList);
            model.setDescription(description);

            // XML エスケープ
            if(content.indexOf(Constants.ESCAPE_XML_START_PRE_STRING) != -1 && content.indexOf(Constants.ESCAPE_XML_END_PRE_STRING) != -1) {

                String beforeHtml = "";
                String afterHtml = "";
                String[] tmpHtml1 = content.split(Constants.ESCAPE_XML_START_PRE_STRING);
                beforeHtml = tmpHtml1[0];
                String[] tmpHtml2 = tmpHtml1[1].split(Constants.ESCAPE_XML_END_PRE_STRING);
                if(tmpHtml2.length > 1) {
                    afterHtml = tmpHtml2[1];
                }
                String escContentXml = StringEscapeUtils.escapeXml(tmpHtml2[0]);

                String escContent = beforeHtml +
                    Constants.ESCAPE_XML_START_PRE_STRING +
                    escContentXml +
                    Constants.ESCAPE_XML_END_PRE_STRING + afterHtml;

                model.setContent(new Text(escContent));

            }else {
                model.setContent(new Text(content));
            }

            // Update
            articleService.update(model);
        }

        return redirect("/admin/article/articleEdit?article=" + article);
    }


    private List<String> getStringList(String texts) {

        List<String> stringList = new ArrayList<String>();
        if(texts != null && texts != "") {
            String[] amznWdgtAsinsTmp = texts.split(",");
            for(String txt: amznWdgtAsinsTmp) {
                if(!txt.isEmpty()) {
                    stringList.add(txt);
                }
            }
        }
        return stringList;
    }
}
